package com.springboot.demo.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.cx.item.common.utils.FileUtil;
import com.springboot.demo.mapper.AttachmentFileMapper;
import com.springboot.demo.model.example.AttachmentFileExample;
import com.springboot.demo.model.model.AttachmentFile;
import com.springboot.demo.service.AttachmentFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class AttachmentFileServiceImpl extends BaseServiceImpl<AttachmentFile, AttachmentFileExample, AttachmentFileMapper> implements AttachmentFileService {

    /**
     * 文件上传路径
     */
    @Value("${upload.file.path}")
    private String uploadFilePath;

    @Autowired
    private AttachmentFileMapper attachmentFileMapper;

    @Override
    public List<Map<String, String>> getFileList(Integer sourceId, Integer sourceType) {

        // 图片
        AttachmentFileExample fileExample = new AttachmentFileExample();
        AttachmentFileExample.Criteria fileExampleCriteria = fileExample.createCriteria();
        fileExampleCriteria.andFileStatusEqualTo(1);
        fileExampleCriteria.andSourceIdEqualTo(sourceId);
        fileExampleCriteria.andSourceTypeEqualTo(sourceType);
        List<AttachmentFile> attachmentFileList = attachmentFileMapper.selectByExample(fileExample);

        List<Map<String, String>> resultList = new ArrayList<>();
        if (CollUtil.isNotEmpty(attachmentFileList)) {
            for (AttachmentFile file : attachmentFileList) {
                Map<String, String> fileMap = new HashMap<>();
                fileMap.put("fileUuid", file.getFileUuid());
                fileMap.put("filePath", file.getFilePath());
                resultList.add(fileMap);
            }
        }

        return resultList;
    }

    @Override
    public int updateFileByUuidList(List<String> uuidList, Integer fileStatus, Integer sourceId, Integer sourceType) {

        int i = attachmentFileMapper.updateFileByUuidList(uuidList, fileStatus, sourceId, sourceType);

        return i;
    }

    @Override
    public Map<String, String> uploadFile(MultipartFile multipartFile) throws Exception {

        Map<String, String> uploadFile = FileUtil.uploadFile(multipartFile.getOriginalFilename(), uploadFilePath, multipartFile.getInputStream());

        // 添加文件记录信息
        AttachmentFile attachmentFileMod = new AttachmentFile();
        attachmentFileMod.setFileUuid(uploadFile.get("fileUuid"));
        attachmentFileMod.setFileType(uploadFile.get("fileType"));
        attachmentFileMod.setFileName(uploadFile.get("fileName"));
        attachmentFileMod.setFileSize(Integer.parseInt(uploadFile.get("fileSize")));
        attachmentFileMod.setFilePath(uploadFile.get("filePath"));
        attachmentFileMod.setFileAbsolutePath(uploadFile.get("fileAbsolutePath"));
        attachmentFileMod.setCreateTime(new Date());
        attachmentFileMod.setFileStatus(1); // 0删除,1正常
        attachmentFileMapper.insertSelective(attachmentFileMod);

        uploadFile.remove("fileAbsolutePath");
        return uploadFile;
    }


    @Override
    public Map<String, String> uploadImage(MultipartFile multipartFile) throws Exception {

        Map<String, String> uploadFile = FileUtil.uploadFile(multipartFile.getOriginalFilename(), uploadFilePath, multipartFile.getInputStream());
        return uploadFile;
    }

}